import 'dart:convert';

import 'package:flutter/material.dart';

abstract class IValueOption {
  String getLabel();

  dynamic getValue();
}

class ValueOption<T> extends IValueOption {
  String label;
  T value;

  ValueOption(this.label, this.value);

  @override
  String getLabel() {
    return label;
  }

  @override
  getValue() {
    return value;
  }
}

class CustomRadio extends StatelessWidget
    implements CustomFormItem<IValueOption> {
  bool? required = false;
  String label;
  double labelMaxWidth;
  List<IValueOption> options;
  WrapAlignment inputAlignment;
  dynamic groupValue;
  TextStyle? labelStyle;
  TextStyle? valueStyle;
  String? hintText;
  bool enable;
  List<Widget>? rightAction;
  Function(dynamic)? onChanged;

  CustomRadio(this.label, this.options,
      {super.key,
      this.groupValue,
      this.required,
      this.labelStyle,
      this.labelMaxWidth = 100,
      this.valueStyle,
      this.inputAlignment = WrapAlignment.end,
      this.rightAction,
      this.hintText,
      this.enable = true,
      this.onChanged});

  @override
  Widget build(BuildContext context) {
    List<Widget> list = [];
    if (required == true) {
      list.add(const SizedBox(
        width: 10,
        child: Center(
          child: Text(
            "*",
            style: TextStyle(
              fontSize: 15,
              color: Color(0xFFFF685C),
            ),
          ),
        ),
      ));
    } else {
      // list.add(const SizedBox(
      //   width: 16,
      // ));
    }
    // list.add(Text(
    //   label,
    //   style:
    //       labelStyle ?? const TextStyle(color: Color(0xff333333), fontSize: 15),
    // ));
    list.add(Container(
      constraints: BoxConstraints(maxWidth: labelMaxWidth),
      child: Text(
        label,
        style: labelStyle ??
            const TextStyle(color: Color(0xff666666), fontSize: 15),
      ),
    ));
    list.add(const SizedBox(
      width: 4,
    ));
    List<Widget> inputList = [];
    for (var e in options) {
      inputList.add(IntrinsicWidth(
        child: Row(
          children: [
            SizedBox(
              width: 24,
              height: 24,
              child: Radio(
                value: e.getValue(),
                activeColor: Colors.blue,
                groupValue: groupValue,
                onChanged: (value) {
                  if (enable) {
                    groupValue = value;
                    onChanged?.call(value);
                  }
                },
              ),
            ),
            const SizedBox(
              width: 1,
            ),
            Text(
              e.getLabel(),
              style: const TextStyle(fontSize: 14),
            ),
          ],
        ),
      ));
    }

    list.add(Expanded(
        child: Wrap(
      alignment: inputAlignment,
      children: inputList,
    )));

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const SizedBox(
          height: 48,
          width: 0,
        ),
        ...list
      ],
    );
  }

  @override
  String getFullValue() {
    var value = getTypeValue();
    if (value != null) {
      return json.encoder.convert(value);
    }
    return "";
  }

  @override
  IValueOption? getTypeValue() {
    return options.firstWhere((element) => element.getValue() == groupValue);
  }

  @override
  String getValue() {
    return groupValue.toString();
  }
}

abstract class CustomFormItem<V> {
  String getValue();

  String getFullValue();

  V? getTypeValue();
}
